
require('faker')
require('xxtsp')
require('awz')



local curl = require('lcurl')
local e = curl.easy()

function atexit(callback) -- 参数为一个函数，使用 atexit(一个函数) 注册一个函数在脚本结束时执行，建议不要耗时太长
	____atexit_guard____ = ____atexit_guard____ or {}
	if type(____atexit_guard____) == 'table' then
		if not getmetatable(____atexit_guard____) then
			setmetatable(____atexit_guard____, {
					__gc = function(self)
						if type(self.callback) == 'function' then
							pcall(self.callback)
						end
					end
				})
		end
		____atexit_guard____.callback = callback
	else
		error('别用 `____atexit_guard____` 命名你的变量。')
	end
end

atexit(function() 
		sys.toast('脚本结束了！')
--		vpnx()
		local appbids = app.front_bid()
		if appbids ~= "com.apple.springboard" then
			app.quit(appbids)
			--closeX(appbids)
		end
		sys.msleep(500)
	end)

function rd(n,k)
	return math.random(n,k)
end

function appname(bid)
	return app.localized_name(bid) or '未安装'
end

-- 总的脚本, 用渠道名来区分脚本
function get_task()
	local url = 'http://wenfree.cn/api/Public/tjj/?service=Tjj.gettask'
	local postArr = {}
	postArr.phonename = phonename or device.name()
	postArr.imei = phoneimei or sys.mgcopyanswer("SerialNumber")
	local taskData = post(url,postArr)
	
	if taskData ~= nil then
		taskData = json.decode(taskData)
		log(taskData)
		
		if taskData.data == "新增手机" or taskData.data == "暂无任务" then
			log(taskData.data,true)
			delay(30)
			return false
		else
			return taskData.data
		end
	end
end

function task_callback(task_id,success)
	local url = 'http://wenfree.cn/api/Public/idfa/?service=Task.Task_callback'
	local postarr_ = {}
	postarr_['idfa'] = idfa
	local postArr = {}
	postArr.id = task_id
	postArr.success = success
	postArr.arr = json.encode(postarr_)
	log("wenfree")
	post(url,postArr)
	log("上传一次")
end

function checkip()
	local url = 'http://wenfree.cn/api/Public/idfa/?service=ip.checkip'
	local getdata = get(url)
	if getdata ~= nil then
		local data = json.decode(getdata)
		log(data or "nil")
		if data.data.state == "ok" then
			ip = data.data.ip or get_ip()
			log("ip可以用:OK.",true)
			return true
		else
			log("ip, 排重失败",true)
		end
	else
		log("ip服务器故障")
	end
end

function ends()
	for _,bid in ipairs(app.bundles()) do
		app.quit(bid)
	end
--	vpnx()
	sys.msleep(2000)
end


function vpnx()
uis={}
	uis.denglu={{
	{382, 981, 0xffffff},
	{375, 981, 0x0ec583},
	{474, 849, 0xa9a9a9},
	{262, 856, 0x0ec583},
}, 85, 26, 770, 719, 1078}
	uis.denglu2={{
	{370, 793, 0x0ec583},
	{350, 791, 0xffffff},
	{106, 687, 0x0ec482},
	{520, 399, 0xefefef},
}, 85, 49, 362, 712, 922}

	uis.getIP={{
	{237, 844, 0xffffff},
	{242, 844, 0x0ec583},
	{518, 985, 0xf02c34},
	{677, 955, 0xf02c34},
	{ 68, 975, 0xf02c34},
	{462, 854, 0xa9a9a9},
}, 85, 32, 773, 722, 1051}

	uis.OUt={{
	{378,  983, 0xf02c34},
	{ 66,  951, 0xf02c34},
	{683, 1015, 0xf02c34},
}, 85, 29, 772, 724, 1061}


	local TIMEline = os.time()
	changekey = 1	
	while os.time()- TIMEline < 30 do
		bid = "com.juejin.ios.wuji"
		
		if active(bid, 3) then
			for k,v in pairs(uis) do
				if d(v,k,true) then
					if k == 'OUt' then
						return true
					end
				end
			end
		end
		delay(2)
	end
end


function wujivpn()
	uis={}
	uis.denglu={{
	{382, 981, 0xffffff},
	{375, 981, 0x0ec583},
	{474, 849, 0xa9a9a9},
	{262, 856, 0x0ec583},
}, 85, 26, 770, 719, 1078}
	uis.denglu2={{
	{370, 793, 0x0ec583},
	{350, 791, 0xffffff},
	{106, 687, 0x0ec482},
	{520, 399, 0xefefef},
}, 85, 49, 362, 712, 922}

	uis.getIP={{
	{237, 844, 0xffffff},
	{242, 844, 0x0ec583},
	{518, 985, 0xf02c34},
	{677, 955, 0xf02c34},
	{ 68, 975, 0xf02c34},
	{462, 854, 0xa9a9a9},
}, 85, 32, 773, 722, 1051}

	uis.change={{
	{579, 845, 0x0ec583},
	{575, 845, 0xffffff},
	{673, 990, 0xf02c34},
	{ 93, 994, 0xf02c34},
	{128, 854, 0xff9f37},
}, 85, 29, 781, 733, 1054}

uis.change2={{
	{532,  857, 0x0ec583},
	{568,  851, 0xffffff},
	{ 69,  813, 0x0ec583},
	{685, 1017, 0xf02c34},
	{ 67, 1019, 0xf02c34},
}, 85, 19, 728, 736, 1090}


	local TIMEline = os.time()
	changekey = 1	
	while os.time()- TIMEline < 30 do
		local bidwj = "com.juejin.ios.wuji"
		
		if active(bidwj, 3) then
			for k,v in pairs(uis) do
				if d(v,k,true) then
					if k == 'change' then
						delay(3)
						return true
					end
				end
			end
		end
		delay(2)
	end
end

function gst()
	uis= {}
	uis.同意 = {{
	{552, 1178, 0x4c7afe},{548, 1176, 0xffffff},{ 73, 1144, 0xffffff},
	{126, 1217, 0xffffff},{471, 1132, 0x4c7afe},
	{500, 1217, 0x4c7afe},}, 85, 33, 1094, 732, 1255}
	
	uis.X ={{
	{375, 942, 0xd9b28a},
	{494, 844, 0xc28d54},
	{214, 852, 0xefd8c0},
	{443, 846, 0xfcfcfd},
}, 85, 164, 761, 550, 984}
	
	uis.登录={{
	{182,  164, 0x121c32},
	{ 54,  182, 0xed7374},
	{133,  185, 0xed7374},
	{673, 1291, 0x4e7dff},
}, 85, 20, 108, 731, 1330}

	uis.我的 = {{
	{680, 1291, 0xc1cbdb},
	{673, 1272, 0xf2f5fb},
	{669, 1314, 0xffffff},
}, 85, 639, 1242, 712, 1329}

uis.bjs = {{
	{384, 960, 0x5e7cff},
	{395, 953, 0x5e7cff},
	{389, 955, 0xffffff},
	{601, 933, 0x4e7afe},
	{141, 953, 0x6f7fff},
}, 85, 87, 862, 664, 1063}
	
	local TIMEline = os.time()
	changekey = 1	
	while os.time()- TIMEline < 30 do
--		bid = "com.juejin.ios.wuji"
		
		if active(bid, 3) then
			for k,v in pairs(uis) do
				if d(v,k,true) then
					if k == '登录' then
						delay(2)
						return true
					end
				end
			end
		end
		delay(2)
	end

end



function shzj()
	uis= {}
	uis.同意 = {{
	{528, 1021, 0x3786f6},
	{441, 1221, 0x7d2412},
	{245, 1021, 0x1a1a1a},
	}, 85, 106, 978, 633, 1271}
	
	uis.X ={{
	{659,  82, 0xffffff},
	{689,  78, 0xd8e0e9},
	{694,  72, 0xfbfcfd},
	{635, 203, 0xff5450},
	{213, 366, 0xff806f},
	}, 85, 735, 61, 183, 1255}
	
	uis.登录={{
	{366,  304, 0xfe484a},
	{184,  142, 0xfe484a},
	{318,  318, 0xffffff},
	{671, 1249, 0xfe484a},
	}, 85, 142, 104, 732, 1324}

	uis.我的 = {{
	{673, 1269, 0x1a1a1a},
	{673, 1274, 0xffffff},
	{683, 1281, 0x363636},
	{676, 1304, 0xffffff},
	}, 85,641, 1237, 703, 1329}

	uis.跳过 = {{
		{673,   79, 0xffffff},
		{729,   81, 0xffffff},
		{446, 1167, 0xff0000},
		{414, 1167, 0xff0000},
	}, 85, 0, 0, 0, 0}

	uis.跳过2 = {{
		{692,   78, 0xffffff},
		{297, 1172, 0xf91d1d},
		{451, 1170, 0xabadb1},
	}, 85, 93, 47, 735, 1201}
	
	uis.今日不再提示 = {{
	{184, 936, 0xfe484a},
	{209, 928, 0xffffff},
	{425, 942, 0xff4325},
	{643, 924, 0xff6338},
}, 85, 61, 879, 689, 997}
	
	local TIMEline = os.time()
	changekey = 1	
	while os.time()- TIMEline < 30 do
--		bid = "com.juejin.ios.wuji"
		
		if active(bid, 3) then
			for k,v in pairs(uis) do
				if d(v,k,true) then
					if k == '登录' then
						delay(2)
						return true
					end
				end
			end
		end
		delay(2)
	end

end

function back_pass(task_id,success)
	local url = 'http://wenfree.cn/api/Public/tjj/?service=Tjj.backpass'
	local postArr = {}
	postArr.task_id = task_id
	postArr.success = success
	log( post(url,postArr) )
end

function upIDFA(name)
	require("ipList")
	
	ip = iplist[rd(1, #iplist)] .. rd(1,223)
	local url = 'http://wenfree.cn/api/Public/idfa/?service=idfa.idfa'
	local postArr ={}
	postArr.phonename = phonename or device.name()
	postArr.phoneimei = phoneimei or sys.mgcopyanswer("SerialNumber")
	postArr.name = name
	postArr.idfa = idfa or deviceInfo.IDFA
	postArr.phoneos = sys.version()
	postArr.ip = ip
--	log(url)
	log("准备上传")
	local taskData = post(url,postArr)
--	log(json.encode(postArr))
--	log(taskData)
	log('上传结束')
end


function all()

	local TaskDate = ( get_task() )
	if TaskDate then
		
		for i,v in ipairs(TaskDate) do
--			wujivpn()
			
			bid = v.appbid
			if v.work == "国盛通-开户炒股买基金" then
				if gst() then
					idfa = yjxj(bid)
					delay(8)
					sys.toast('准备上传', 3000)
					upIDFA(v.work)
					back_pass(v.task_id, 'ok')
				else
					idfa = yjxj(bid)
					delay(5)
				end
			elseif v.work == "上海证券指e通" then
				wujivpn()
				
				if shzj() then
					idfa = yjxj(bid)
					delay(8)
					sys.toast('准备上传', 3000)
					upIDFA(v.work)
					back_pass(v.task_id, 'ok')
				else
					idfa = yjxj(bid)
					delay(5)
				end
				
			elseif v.work == "西部证券" then
				delay(5)
				require('xbzq')
				if xbzq() then
					idfa = yjxj(bid)
					delay(8)
					sys.toast('准备上传', 3000)
					upIDFA(v.work)
					back_pass(v.task_id, 'ok')
				else
					idfa = yjxj(bid)
					delay(5)
				end
			elseif v.work == '上海静安' then
				require('shja')
				if AWZnew() then
					wujivpn()
					if shja() then
	--					idfa = yjxj(bid)
						delay(5)
						sys.toast('准备上传', 3000)
						upIDFA(v.work)
						back_pass(v.task_id, 'ok')
					else
--						idfa = yjxj(bid)
						delay(5)
					end
				end
				vpnx()
			end
--			vpnx()
		end
	else
--        vpnx()
		vpnconf.disconnect()
	end
	------------------------------------
--	ends()
end

--vpnx()

while (true) do
	local ret,errMessage = pcall(all)
	if ret then
	else
		log(errMessage)
		sys.alert(json.encode(errMessage), 8)
--		vpnx()
		vpnconf.disconnect()
		delay(5)
	end
end



--bid = "com.tzt.xbzq3.0"
--idfa = yjxj(bid)

--idfa = "4766BAD2-EE7E-4A5E-8FA5-A43DFB5D223C"
--upIDFA("wuji")



























